<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'include/sql/class_db.php';
require_once ROOT_PATH.'include/sql/base.sql.php';

class FeedSqlFunc extends BaseSqlFunc
{
	/**
	 * 
	 * 根据uid获取动态信息列表
	 * @param array $request
	 */
	function get_feed_count($uid)
	{
		$time = time() - 10*60;
		$sql = <<<SQL
select
	count(*)
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	(f.f_user_id=?
	or s.user_id=?)
	and f.create_time > ?
SQL;

		$count = DBFunction::getInstance()->getOne($sql, array($uid, $uid, $time));
		if ($count < 1) return array();
		
		$sql = <<<SQL
select
	f.id, f.`type`
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	(f.f_user_id=?
	or s.user_id=?)
	and f.create_time > ?
order by f.create_time desc
SQL;

		$list = DBFunction::getInstance()->getAll($sql, array($uid, $uid, $time));
		
		return $list;
	}
	
	/**
	 * 
	 * 根据uid获取动态信息列表
	 * @param array $request
	 */
	function get_list_by_uid($request)
	{
		$limit = $request['page_size'];
		$num = $request['page_index'];

			$sql = <<<SQL
select
	count(*)
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	f.f_user_id=?
SQL;

		$count = DBFunction::getInstance()->getOne($sql, array($request['uid']));
		if ($count < 1) return array();
		
		$sql = <<<SQL
select
	f.id, f.user_id, f.`type`, u.`name`, u.`image`, u.`gender`,
	u1.id as "fuid", u1.name as "fname", u1.image as "fimage", u1.gender as "fgender",
	s.id as "story_id", s.title, s.image1
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	f.f_user_id=?
order by f.create_time desc
SQL;

		$list = DBFunction::getInstance()->selectLimit($sql, $limit, $num, array($request['uid']));
		
		$result['total_count'] = $count;
		$result['list'] = $list;
		
		return $result;
	}
	
	/**
	 * 
	 * 根据uid获取用户关注动态信息列表
	 * @param array $request
	 */
	function get_follow_list_by_uid($request)
	{
		$limit = $request['page_size'];
		$num = $request['page_index'];

		$sql = <<<SQL
select
	count(*)
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user_follow` uf on u.id = uf.f_user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	uf.user_id=?
SQL;

		$count = DBFunction::getInstance()->getOne($sql, array($request['uid']));
		if ($count < 1) return array();
		
		$sql = <<<SQL
select
	f.id, f.user_id, f.`type`, u.`name`, u.`image`, u.`gender`,
	u1.id as "fuid", u1.name as "fname", u1.image as "fimage", u1.gender as "fgender",
	s.id as "story_id", s.title, s.image1
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user_follow` uf on u.id = uf.f_user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	uf.user_id=?
order by f.id desc
SQL;

		$list = DBFunction::getInstance()->selectLimit($sql, $limit, $num, array($request['uid']));
		
		$result['total_count'] = $count;
		$result['list'] = $list;
		
		return $result;
	}
	
	/**
	 * 
	 * 根据ID获取动态信息
	 * @param string $id
	 */
	function get($id)
	{
		$sql = <<<SQL
select
	f.id, f.user_id, f.`type`, u.`name`, u.`image`, u.`gender`,
	u1.id as "fuid", u1.name as "fname", u1.image as "fimage", u1.gender as "fgender",
	s.id as "story_id", s.title
from
	`{$this->pre}user_feed` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	left join `{$this->pre}user` u1 on u1.id = f.f_user_id
	left join `{$this->pre}story` s on s.id = f.f_story_id
where
	f.id=?
SQL;

		$result = DBFunction::getInstance()->getRow($sql, array($id));

		return check_array($result) ? $result : array();
	}
	
	/**
	 * 
	 * 新增动态
	 * @param array $request
	 */
	function add($request)
	{
		$fuid = isset($request['fuid']) ? $request['fuid'] : '';
		$fsid = isset($request['fsid']) ? $request['fsid'] : '';
		
		$params = array(
		$request['uid'],
		$request['type'],
		$fuid,
		$fsid,
		$request['time']
		);

		$sql = <<<SQL
insert into `{$this->pre}user_feed`
	(`user_id`, `type`, `f_user_id`, `f_story_id`, `create_time`)
values
	(?,?,?,?,
	?)
SQL;

		$result = DBFunction::getInstance()->insert($sql, $params);
		
		return ($result !== false) ? $result : false;
	}
	
	/**
	 * 
	 * 更新动态
	 * @param array $request
	 */
	function update($request)
	{
		return false;
	}
	
	/**
	 * 
	 * 删除动态
	 * @param array $request
	 */
	function delete($request)
	{
		return false;
	}
}